package com.isunland.managesystem.base;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;

/**
 * 自定义了提示信息,及两个Button的显示文字.
 * 如果点击了不同的button,回传不同的操作码.
 *
 * @author wangyanan
 */
public class SimpleDialogFragment extends DialogFragment {

  private static final String EXTRA_HINT_ID = "com.isunland.managesystem.extra_hint_id";
  private static final String EXTRA_POSITIVE_ID = "com.isunland.managesystem.EXTRA_POSITIVE_ID";
  private static final String EXTRA_NAVIGATE_ID = "com.isunland.managesystem.EXTRA_NAVIGATE_ID";
  private static final String EXTRA_NEUTRAL_ID = "com.isunland.managesystem.EXTRA_NEUTRAL_ID";
  public static final String EXTRA_CHOICE = "com.isunland.managesystem.EXTRA_CHOICE";
  public static final int VALUE_POSITIVE = 1;
  public static final int VALUE_NAVIGATE = 2;
  public static final int VALUE_NEUTRAL = 3;

  /**
   * id如果不需要,传值传0.
   *
   * @param hintStringId 提示id
   * @param positiveStringId 正按钮id
   * @param neutralStringId 中立按钮id
   * @param navigateStringId 反按钮id.
   */
  public static SimpleDialogFragment newInstance(int hintStringId, int positiveStringId,
      int neutralStringId, int navigateStringId) {
    Bundle argsBundle = new Bundle();
    argsBundle.putInt(EXTRA_HINT_ID, hintStringId);
    argsBundle.putInt(EXTRA_POSITIVE_ID, positiveStringId);
    argsBundle.putInt(EXTRA_NAVIGATE_ID, navigateStringId);
    argsBundle.putInt(EXTRA_NEUTRAL_ID, neutralStringId);
    SimpleDialogFragment fragment = new SimpleDialogFragment();
    fragment.setArguments(argsBundle);
    return fragment;
  }

  private int mHintId;
  private int mPositiveId;
  private int mNavigateId;
  private int mNeutralId;

  @Override public void onCreate(Bundle savedInstanceState) {
    mHintId = getArguments().getInt(EXTRA_HINT_ID);
    mPositiveId = getArguments().getInt(EXTRA_POSITIVE_ID);
    mNavigateId = getArguments().getInt(EXTRA_NAVIGATE_ID);
    mNeutralId = getArguments().getInt(EXTRA_NEUTRAL_ID);
    super.onCreate(savedInstanceState);
  }

  @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) {

    Builder builder = new AlertDialog.Builder(getActivity()).setMessage(mHintId);

    if (mPositiveId != 0) {
      builder.setPositiveButton(mPositiveId, new OnClickListener() {

        @Override public void onClick(DialogInterface dialog, int which) {
          onClickMethod(VALUE_POSITIVE);
        }
      });
    }
    if (mNavigateId != 0) {

      builder.setNegativeButton(mNavigateId, new OnClickListener() {

        @Override public void onClick(DialogInterface dialog, int which) {
          onClickMethod(VALUE_NAVIGATE);
        }
      });
    }

    if (mNeutralId != 0) {
      builder.setNeutralButton(mNeutralId, new OnClickListener() {

        @Override public void onClick(DialogInterface dialog, int which) {
          onClickMethod(VALUE_NEUTRAL);
        }
      });
    }
    return builder.create();
  }

  public void onClickMethod(int result) {
    if (getTargetFragment() == null) return;
    Intent i = new Intent();
    i.putExtra(EXTRA_CHOICE, result);
    getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, i);
  }
}
